package com.example.campuslink.commen.db.userNumber

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.TypeConverters
import com.example.campuslink.commen.db.InvitationTableConverter
import com.example.campuslink.commen.db.MessageBeanConverter
import com.example.campuslink.commen.db.UserInfoDataConverter
import com.example.campuslink.commen.db.invitation.InvitationTable
import com.example.campuslink.commen.db.userInfo.UserInfoData
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.model.MessageBeanData
import db.ListStringConverter
import java.io.Serializable

@Entity(tableName = "user_number_table", primaryKeys = ["telNumber", "id"])
@TypeConverters(
    ListStringConverter::class,
    InvitationTableConverter::class,
    MessageBeanConverter::class,
    UserInfoDataConverter::class
)
data class UserNumberTable(

    //账号
    var telNumber: String = "",
    //自增id
    val id: Int = 0,
    //密码
    var password: String = "",
    //是否是管理员账号
    var isAdmin: Boolean = false,
    //学校名和学校表双向关联
    var schoolName: String = "",
    //创建时间
    var createTime: Long = 0,
    //粉丝列表
    @TypeConverters(ListStringConverter::class)
    var fansList: MutableList<String> = mutableListOf(),
    //关注列表
    @TypeConverters(ListStringConverter::class)
    var followList: MutableList<String> = mutableListOf(),
    //浏览过的帖子
    @TypeConverters(InvitationTableConverter::class)
    var browseList: MutableList<InvitationTable> = mutableListOf(),
    //收藏的帖子列表
    @TypeConverters(InvitationTableConverter::class)
    var collectList: MutableList<InvitationTable> = mutableListOf(),
    //发布的贴子列表
    @TypeConverters(InvitationTableConverter::class)
    var publishList: MutableList<InvitationTable> = mutableListOf(),
    @TypeConverters(MessageBeanConverter::class)
    var messageList: MutableList<MessageBeanData> = mutableListOf(),
    @ColumnInfo(defaultValue = "") // 必须添加默认值
    val username: String,
    @TypeConverters(UserInfoDataConverter::class)
    var userInfoData: UserInfoTable? = null

    ) : Serializable {
    @Ignore
    constructor() : this(telNumber = "", id = 0,  password = "", username = "",  userInfoData = UserInfoTable()
    , fansList = mutableListOf(),followList = mutableListOf(),browseList= mutableListOf(),collectList= mutableListOf(),
        publishList = mutableListOf(), messageList = mutableListOf()
    )

    companion object {
        suspend fun insertWithId(
            dao: UserNumberDao,
            userNumberTable: UserNumberTable
        ) {
            val maxId = dao.getMaxId() ?: 0
            dao.insertUserNumber(userNumberTable.copy(id = maxId + 1))
        }
    }
}